<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="ism" uri="http://www.baseproj.org/rd/html"%>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<ism:link rel="stylesheet" type="text/css" href="../skins/main.css" debug="false" />
<ism:link rel="stylesheet" type="text/css" href="../js/artDialog4.1.7/skins/green.css" />
</head>
<body class="popUp caseImg chooseTO">
    <div class="dataGrid">
        <table>
         <thead>
            <tr>
            <th>结果名</th>
            <s:iterator value="scheme.questions">
            <th><s:property value="name" /></th>
            </s:iterator>
            </tr>
            </thead>
            <tbody class="tableHover" id="resultBody">
            </tbody>
        </table>
    </div>
    <div class="bottomtoolbar">
        <input class="inputButton" type="button" onclick="modify()" value="确定"> <input class="inputButton subButton" type="button" onclick="popdown('modifyResult');" value="取消">
    </div>
    <div>
            <input type="hidden" name="projectCode" id="projectCode" value="<s:property value='projectCode' />" />
            <input type="hidden" name="schemeCode" id="schemeCode" value="<s:property value='schemeCode' />" />
        <input type="hidden" name="schemeQuestions" id="schemeQuestions" value="<s:property value='@com.rd.sys.common.utils.JacksonMapper@bean2Json(scheme.questions)' />" /> <input type="hidden"
            name="results" id="results" value="<s:property value='@com.rd.sys.common.utils.JacksonMapper@bean2Json(resultList)' />" />
    </div>
</body>
<ism:script type="text/javascript" src="../js/jquery.min.js"></ism:script>
<ism:script type="text/javascript" src="../js/common.js" debug="false"></ism:script>
<ism:script type="text/javascript" src="../js/popSelect.js"></ism:script>
<ism:script type="text/javascript" src="../js/eye_core.js" debug="false"></ism:script>
<ism:script type="text/javascript" src="../js/artDialog4.1.7/artDialog.js"></ism:script>
<ism:script type="text/javascript" src="../js/artDialog4.1.7/plugins/iframeTools.js"></ism:script>
<script type="text/javascript">
    var schemeQuestions = null; //方案的问卷信息
    var results = null; //被试信息
    var row = 0; //被试信息行数
    var column = 0; //被试信息的属性列数
    $(function() {
        schemeQuestions = eval('(' + $("#schemeQuestions").val() + ')'); // 方案所有问卷信息(含属性)
        results = eval('(' + $("#results").val() + ')'); //该方案下所有的被试信息
        loadResultTableBody();
    });
    
    function loadResultTableBody(){
        if (schemeQuestions == null) { // 若方案无问卷信息,则不用加载
            return;
        }
        var isHasAttribute = false; // 是否存在属性
        for ( var j = 0; j < schemeQuestions.length; j++){
            if (schemeQuestions[j].Type == 0) {
                isHasAttribute = true;
                break;
            }            
        }
        if(!isHasAttribute){  // 若方案问卷中不包括属性,则不用加载
            return;
        }
        
        
        var html=""; 
        for ( var i = 0; i < results.length; i++){ //遍历所有被试信息
            row++;
            html+="<tr>";
            html+="<td>"+results[i].ResultName+"</td>";
            var qusetionHtml="";
            var qusetionId=""; //给每个选项id,便于后续获取修改后的值
            var option = null;
            for ( var j = 0; j < schemeQuestions.length; j++){
                if (schemeQuestions[j].Type == 0) { // 当类型为属性时
                    if(i==0){
                        column++; //计算属性的个数,只需在第一次进schemeQuestions循环时计算即可
                    }
                   qusetionHtml="";
                   qusetionId = "r"+i+"c"+j;

                   if(results[i].Answers==null){ //若该被试没有问卷
                       qusetionHtml+="<td></td>";
                   }else{
                       qusetionHtml+="<td>";
                       if(schemeQuestions[j].ShowType == 0){ // 当需要显示为“输入框”
                           qusetionHtml += "<input type='text' class='inputText_min' style='width:120px' id='"+qusetionId+"' value='" + results[i].Answers[j].Value + "' />";
                       }else if(schemeQuestions[j].ShowType == 1){ // 当需要显示为"下拉列表"
                           option = null;
                           qusetionHtml += "<select class='select_min' style='width:80px' id='"+qusetionId+"'>";
                           for ( var k = 0; k < schemeQuestions[j].OptionList.length; k++) { // 根据属性详情构建下拉列表option
                               option = schemeQuestions[j].OptionList[k];
                               qusetionHtml += "<option value='" + option.Key + "'";
                               if (option.Key == results[i].Answers[j].Value) {
                                   qusetionHtml += " selected ";
                               }
                               qusetionHtml += ">" + option.Value + "</option>";
                           }  
                           qusetionHtml += "</select>";                       
                       }else{
                        // 其它ShowType客户端暂未实现,暂时不考虑
                       }
                       qusetionHtml+="</td>";
                   }
                   html+=qusetionHtml;
                }
            }
        }
        
        $("#resultBody").html(html); 
        html+="</tr>";// 改变resultBody的内容显示
    }
    
    /**
     * 执行修改被试信息
     */
    function modify(){
        for(var i=0; i<row;i++){ //更改被试信息
            if(results[i].Answers==null){ //若该被试没有问卷
                continue;
            }
            for(var j=0; j<column;j++){
                results[i].Answers[j].Value = $("#r"+i+"c"+j).val();
            }
        }
        var inputPara = {
                "projectCode" : $("#projectCode").val(),
                "schemeCode" : $("#schemeCode").val(),
                "thePageResultList" : JSON.stringify(results)
                };
        callJsonDataBySync("analysis_modifyResult.action", inputPara, modifySuccessFunction, loadErrorFunction);
    }
    
    /**
     * 修改被试信息成功时的回调
     */
    function modifySuccessFunction(retListJson){
        top.modifyResultHook(); 
        popdown('modifyResult');
    }
    
    /**
     * 修改被试信息错误时的回调
     */
    function loadErrorFunction(status) {
        if (status != "success") {
            popalert({
                "content" : "系统忙，请稍后重试！",
                "icon" : "warning",
                "okVal" : "确定",
                "cancel" : false
            });
        }
    }
</script>
</html>